/**********************************************************************
 * Project : WoWGuildRoster (http://www.codeplex.com/WowGuildRoster/)
 * Module  : DotNetWarcraft.WoWGuildRoster.UI.DNN.WGR_CharEntry
 **********************************************************************
 * 
 * 
 * 
 **********************************************************************
 * License : All code is subject to licensing terms per the Microsoft
 *   Permissive License (Ms-PL) v1.1. For full license information,
 *   refer to the packaged license.txt file.
 **********************************************************************
 * Revision History:
 * -----------------
 * 2007-09-02 -- Cliff Simpkins: Initial Version
 * 2007-09-16 -- Cliff Simpkins: Added Char Stat Population
 * 2007-11-07 -- Cliff Simpkins: Cleaning up the page visually
 **********************************************************************
 */
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Exceptions;

using WGR_Business = DotNetWarcraft.WoWGuildRoster.Business;

namespace DotNetWarcraft.WoWGuildRoster.UI.DNN {
  public partial class WGR_CharEntry : PortalModuleBase {
    #region "Public Properties"
    public bool IsInMemberRole {
      get {
        try {
          if (this.IsInOfficerRole) {
            return true;
          } else {
            return Convert.ToBoolean(this.ViewState[WGR_Business.WGRDnnConstants.VIEWSTATE_IS_IN_MEMBER_ROLE].ToString());
          }
        } catch {
          return false;
        }
      }
    }
    public bool IsInOfficerRole {
      get {
        try {
          return Convert.ToBoolean(this.ViewState[WGR_Business.WGRDnnConstants.VIEWSTATE_IS_IN_OFFICER_ROLE].ToString());
        } catch {
          return false;
        }
      }
    }

    public bool OwnsCharEntry {
      get {
        if (UserId < 1) {
          return false;
        }
        return (UserId == this.DNN_UserID);
      }
    }
    public bool IsInTestDisplayMode {
      get {
        return pnlAdminOptions.Visible;
      }
      set {
        pnlAdminOptions.Visible = value;
      }
    }

    public String RosterErrorMessage {
      set {
        if (value == "") {
          lblErrorMsg.Visible = false;
        } else {
          lblErrorMsg.Visible = true;
        }
        lblErrorMsg.Text = value;
      }
    }
    #endregion
    #region "Private Properties"
    int _urlCharID = Null.NullInteger;

    protected String[] OfficerRoles {
      get {
        if (Settings.Contains(WGR_Business.WGRDnnConstants.SETTING_OFFICER_ROLES)) {
          return Settings[WGR_Business.WGRDnnConstants.SETTING_OFFICER_ROLES].ToString().Split(';');
        } else {
          return PortalSettings.AdministratorRoleId.ToString().Split(';');
        }
      }
    }
    protected String[] MemberRoles {
      get {
        if (Settings.Contains(WGR_Business.WGRDnnConstants.SETTING_MEMBER_ROLES)) {
          return Settings[WGR_Business.WGRDnnConstants.SETTING_MEMBER_ROLES].ToString().Split(';');
        } else {
          return PortalSettings.AdministratorRoleId.ToString().Split(';');
        }
      }
    }

    protected bool DisplayTestingMode {
      get {
        if (Settings.Contains(WGR_Business.WGRDnnConstants.SETTING_DISPLAY_TESTING_MODE)) {
          try {
            return Convert.ToBoolean(Settings[WGR_Business.WGRDnnConstants.SETTING_DISPLAY_TESTING_MODE].ToString());
          } catch {
            return false;
          }
        } else {
          return false;
        }
      }
    }

    protected int Char_ID {
      get {
        try {
          if (_urlCharID == Null.NullInteger) {
            if (!(Request.Params["CharID"] == null)) {
              _urlCharID = Int32.Parse(Request.Params["CharID"]);
              } else {
              _urlCharID = Null.NullInteger;
              }
            }
          } catch {
            _urlCharID = Null.NullInteger;
          }

        return _urlCharID;
        }
      }

    protected String Char_Name {
      get {
        return hlChar_Name.Text;
      }
      set {
        hlChar_Name.Text = value;
      }
    }
    protected String Char_ClassInfo {
      get {
        return lblChar_ClassInfoValue.Text;
      }
      set {
        lblChar_ClassInfoValue.Text = value;
      }
    }
    protected String Char_Guild {
      get {
        return lblChar_GuildValue.Text;
      }
      set {
        lblChar_GuildValue.Text = value;
      }
    }
    protected String Char_RaceGender {
      get {
        return lblChar_RaceGenderInfoValue.Text;
      }
      set {
        lblChar_RaceGenderInfoValue.Text = value;
      }
    }
    protected String Char_Server {
      get {
        return lblChar_ServerValue.Text;
      }
      set {
        lblChar_ServerValue.Text = value;
      }
    }
    protected String Char_Guild_Rank {
      get {
        return lblChar_Guild_RankValue.Text;
      }
      set {
        lblChar_Guild_RankValue.Text = value;
      }
    }
    protected String WoW_Guild_Notes {
      get {
        return lblWoW_Guild_NotesValue.Text;
      }
      set {
        lblWoW_Guild_NotesValue.Text = value;

        if (value.Trim() == "") {
          trWowGuildNotes.Visible = false;
          }
      }
    }
    protected String WoW_Officer_Notes {
      get {
        return lblWoW_Officer_NotesValue.Text;
      }
      set {
        lblWoW_Officer_NotesValue.Text = value;

        if (value.Trim() == "") {
          trWowOfficerNotes.Visible = false;
          }
        }
    }
    protected String WoW_Last_Login {
      get {
        return lblWoW_Last_LoginValue.Text;
      }
      set {
        lblWoW_Last_LoginValue.Text = value;
      }
    }
    protected String WoW_Last_Load_Time {
      get {
        return lblWowLastLoadTimeValue.Text;
      }
      set {
        lblWowLastLoadTimeValue.Text = value;
      }
    }
    protected String WoW_Last_Load_By {
      get {
        return lblWowLastLoadByValue.Text;
      }
      set {
        lblWowLastLoadByValue.Text = value;
      }
    }
    protected String WoW_Last_Update_Time {
      get {
        return lblWowLastUpdateTimeValue.Text;
      }
      set {
        lblWowLastUpdateTimeValue.Text = value;
      }
    }
    protected String WoW_Last_Update_By {
      get {
        return lblWowLastUpdateByValue.Text;
      }
      set {
        lblWowLastUpdateByValue.Text = value;
      }
    }
    //protected String DNN_Player_Notes {
    //  get {
    //    return txtDNN_Player_Notes.Text;
    //  }
    //  set {
    //    txtDNN_Player_Notes.Text = value;
    //  }
    //}
    //protected String DNN_Officer_Notes {
    //  get {
    //    return txtDNN_Officer_Notes.Text;
    //  }
    //  set {
    //    txtDNN_Officer_Notes.Text = value;
    //  }
    //}
    protected bool Primary_Char {
      get {
        return cbPrimary_Char.Checked;
      }
      set {
        cbPrimary_Char.Checked = value;
      }
    }

    protected String Alt_to_Main_String {
      get {
        return dropAlt_to_Main.SelectedValue;
      }
      set {
        if (dropAlt_to_Main.Items.Count < 1) {
          this.BindAltToMainList();
        }

        if (dropAlt_to_Main.SelectedItem != null) {
          dropAlt_to_Main.SelectedItem.Selected = false;
        }
        ListItem _li = dropAlt_to_Main.Items.FindByValue(value);

        if (_li == null) {
          dropAlt_to_Main.Items.FindByValue("-1").Selected = true;
        } else {
          _li.Selected = true;
        }
      }
    }
    protected int Alt_to_MainID {
      get {
        if (this.Alt_to_Main_String.Trim() != "") {
          try {
            return int.Parse(Alt_to_Main_String);
          } catch {
            return -1;
          }
        } else {
          return -1;
        }
      }
      set {
        if (value >= 0) {
          Alt_to_Main_String = value.ToString();
        } else {
          Alt_to_Main_String = "";
        }
      }
    }

    protected String DNN_User_String {
      get {
        return dropDNN_User.SelectedValue;
      }
      set {
        if (dropDNN_User.Items.Count < 1) {
          this.BindDnnUserList();
        }

        if (dropDNN_User.SelectedItem != null) {
          dropDNN_User.SelectedItem.Selected = false;
        }
        ListItem _li = dropDNN_User.Items.FindByValue(value);

        if (_li == null) {
          dropDNN_User.Items.FindByValue("-1").Selected = true;
        } else {
          _li.Selected = true;
        }
      }
    }
    protected int DNN_UserID {
      get {
        if (this.DNN_User_String.Trim() != "") {
          try {
            return int.Parse(DNN_User_String);
          } catch {
            return -1;
          }
        } else {
          return -1;
        }
      }
      set {
        if (value >= 0) {
          DNN_User_String = value.ToString();
        } else {
          DNN_User_String = "";
        }
      }
    }
    #endregion

    protected WGR_Business.GuildEntry GetCharacterData() {
      WGR_Business.GuildEntry _char = new WGR_Business.GuildEntry();
      if (!Null.IsNull(this.Char_ID)) {
        return WGR_Business.DnnGuildRosterController.GetCharEntry(this.Char_ID);
        } else {
        return null;
        }

      }

    protected void Page_Load(object sender, EventArgs e) {
      try {
        this.hlDNW_WGR_site.Text = WGR_Business.DnnGuildRosterController.ApplicationName;
        this.hlDNW_WGR_site.NavigateUrl = this.EditUrl("About");

        if (DotNetNuke.Framework.AJAX.IsInstalled()) {
          DotNetNuke.Framework.AJAX.RegisterScriptManager();
        }

        WGR_Business.GuildEntry _char = new WGR_Business.GuildEntry();

        if (!Page.IsPostBack) {
          cmdDelete.Attributes.Add("onClick", "javascript:return confirm('" + Localization.GetString("DeleteItem") + "');");
          if (!Null.IsNull(this.Char_ID)) {
            _char = GetCharacterData();
            if (_char != null) {
              this.LoadData_CharInfo(ref _char);
              } else {
              Response.Redirect(Globals.NavigateURL(), true);
            }

            this.IsInTestDisplayMode = this.DisplayTestingMode;
            this.RetrieveVisibilityRoles();
            this.TrimVisibility();

            this.BindAltList();
          }
        }
      } catch (Exception exc) {
        Exceptions.ProcessModuleLoadException(this, exc);
      }

    }
    private void Page_Init(object sender, System.EventArgs e) {

      this.hlDNW_WGR_site.Text = WGR_Business.DnnGuildRosterController.ApplicationName;
      this.hlDNW_WGR_site.NavigateUrl = this.EditUrl("About");

      WGR_Business.GuildEntry _char = GetCharacterData();
      if (_char != null) {
        this.ModuleConfiguration.ModuleTitle = _char.Char_Name;
        this.ModuleConfiguration.ModuleTitle += " (Level " + _char.Char_Level.ToString() + " " + _char.Char_Class + ")";

        }
      } 

    #region Private Methods
    private void RetrieveVisibilityRoles() {
      bool _IsInMemberRole = false;
      bool _IsInOfficerRole = false;

      if (this.IsInTestDisplayMode) {
        _IsInMemberRole = cbTestDisplay_Members.Checked;
        _IsInOfficerRole = cbTestDisplay_Officers.Checked;
      } else {
        DotNetNuke.Security.Roles.RoleController objCtrl = new DotNetNuke.Security.Roles.RoleController();

        foreach (String _role in this.MemberRoles) {
          try {
            if (_role != "" && DotNetNuke.Security.PortalSecurity.IsInRole(objCtrl.GetRole(int.Parse(_role), this.PortalId).RoleName)) {
              _IsInMemberRole = true;
            }
          } catch {
          }
        }

        foreach (String _role in this.OfficerRoles) {
          try {
            if (_role != "" && DotNetNuke.Security.PortalSecurity.IsInRole(objCtrl.GetRole(int.Parse(_role), this.PortalId).RoleName)) {
              _IsInOfficerRole = true;
            }
          } catch {
          }
        }
      }
      this.ViewState[WGR_Business.WGRDnnConstants.VIEWSTATE_IS_IN_MEMBER_ROLE] = _IsInMemberRole.ToString();
      this.ViewState[WGR_Business.WGRDnnConstants.VIEWSTATE_IS_IN_OFFICER_ROLE] = _IsInOfficerRole.ToString();

    }

    private void TrimVisibility() {
      bool _officer = this.IsInOfficerRole;
      bool _member = this.IsInMemberRole;
      bool _changeRights = (_officer || (_member && this.OwnsCharEntry));

      //trDnnOfficerNotes.Visible = _officer;
      trWowOfficerNotes.Visible = _officer && trWowOfficerNotes.Visible;
      trDNN_User.Visible = _officer;

      //trDnnPlayerNotes.Visible = _member;
      trWowGuildNotes.Visible = _member && trWowGuildNotes.Visible;

      dropAlt_to_Main.Visible = _changeRights;
      trAltChars.Visible = this.Primary_Char;
      if (!this.Primary_Char && this.Alt_to_MainID > 0) {
        trAltToMain.Visible = true;

        this.lnkMainCharacter.NavigateUrl = this.EditUrl("CharID", this.Alt_to_Main_String, "ViewChar");
        this.lnkMainCharacter.Text = this.dropAlt_to_Main.SelectedItem.Text;
        this.lnkMainCharacter.Visible = true;
      } else if (this.Alt_to_MainID <= 0) {
        trAltToMain.Visible = _member;
        lnkMainCharacter.Visible = false;
      } else {
        trAltToMain.Visible = true;
        this.lnkMainCharacter.Visible = false;
      }

      cmdThisIsMyMain.Visible = (this.OwnsCharEntry && !this.Primary_Char);

      trPrimaryChar.Visible = _member;

      dropAlt_to_Main.Enabled = _changeRights;
      cbPrimary_Char.Enabled = _changeRights;
      //txtDNN_Player_Notes.Enabled = _changeRights;


      //if (!_changeRights) {
      //  //cmdCancel.Text = "Return to Roster";
      //  cmdCancel.Attributes["resourcekey"] = "cmdReturnToRoster";
      //} else {
      //  //cmdCancel.Text = "Cancel";
      //  cmdCancel.Attributes["resourcekey"] = "cmdCancel";
      //}
      cmdUpdate.Visible = _changeRights;

      if (this.DNN_UserID < 0) {
        cmdClaimEntry.Visible = (!UserInfo.IsSuperUser && _member);
      } else {
        cmdClaimEntry.Visible = false;
      }

      //cmdDelete.Visible = _officer;
    }

    private void BindDnnUserList() {
      //DotNetNuke.Entities.Users.UserController _uCtrl = new DotNetNuke.Entities.Users.UserController();

      dropDNN_User.Items.Clear();
      ListItem _liBlank = new ListItem("None Selected", "-1");
      dropDNN_User.Items.Insert(0, _liBlank);

      //System.Collections.ArrayList _userList = _uCtrl.GetUsers(this.PortalId, false, false);
      List<WGR_Business.DropDownVal> _userList = WGR_Business.DnnGuildRosterController.GetDropDown_DnnUsers(this.PortalId);

      foreach (WGR_Business.DropDownVal _user in _userList) {
        ListItem _li = new ListItem(_user.Value, _user.Key);
        dropDNN_User.Items.Add(_li);
      }

    }

    private void BindStatList1DropDown() {
      int _char_ID = Int32.Parse(Request.Params["CharID"]);

      if (_char_ID > 0) {

        drpStatList1.Items.Add(new ListItem("Base Stats", WGR_Business.WGRDnnConstants.ASCX_CHARENTRY_STATTYPE_BASE));
        drpStatList1.Items.Add(new ListItem("Melee", WGR_Business.WGRDnnConstants.ASCX_CHARENTRY_STATTYPE_MELEE));
        drpStatList1.Items.Add(new ListItem("Spell", WGR_Business.WGRDnnConstants.ASCX_CHARENTRY_STATTYPE_SPELL));
        drpStatList1.Items.Add(new ListItem("Ranged", WGR_Business.WGRDnnConstants.ASCX_CHARENTRY_STATTYPE_RANGED));
        drpStatList1.Items.Add(new ListItem("Defense", WGR_Business.WGRDnnConstants.ASCX_CHARENTRY_STATTYPE_DEFENSE));

      } else {
        drpStatList1.Visible = false;
      }
    }

    private void BindStatList2DropDown() {
      int _char_ID = Int32.Parse(Request.Params["CharID"]);

      if (_char_ID > 0) {
        List<String> _repGrps = WGR_Business.DnnGuildRosterController.GetDropDown_ReputationGroups(_char_ID);

        foreach (String _repGrp in _repGrps) {
          drpStatList2.Items.Add(_repGrp);
        }
      } else {
        drpStatList2.Visible = false;
      }
    }

    private void BindAltToMainList() {

      dropAlt_to_Main.Items.Clear();
      ListItem _liBlank = new ListItem("None Selected", "-1");
      dropAlt_to_Main.Items.Insert(0, _liBlank);

      List<WGR_Business.DropDownVal> _userList = WGR_Business.DnnGuildRosterController.GetDropDowns_MainChars(this.ModuleId);

      foreach (WGR_Business.DropDownVal _char in _userList) {
        ListItem _li = new ListItem(_char.Value, _char.Key);
        dropAlt_to_Main.Items.Add(_li);
      }

    }

    private void BindAltList() {

      int _char_ID = Int32.Parse(Request.Params["CharID"]);
      WGR_Business.GuildEntry _entry = WGR_Business.DnnGuildRosterController.GetCharEntry(_char_ID);
      rstAltList.LoadType = WGR_CharList.RosterLoadType.ShowAltChars;

      if (_entry.AltCharacters == null || _entry.AltCharacters.Count < 1) {
        lblNoAlts.Visible = true;
        rstAltList.Visible = false;
      } else {
        lblNoAlts.Visible = false;
        rstAltList.Visible = true;

        rstAltList.Title = _entry.Char_ID.ToString();
        rstAltList.LoadType = WGR_CharList.RosterLoadType.ShowAltChars;
        rstAltList.ParentModuleID = this.ModuleId;
        rstAltList.ParentPortalID = this.PortalId;
        rstAltList.ListSize = 20;
        rstAltList.ShowMainsOnly = false;
        rstAltList.ShowTitle = false;
      }

    }
    private void UpdateEntryToDB() {
      int _char_ID = Int32.Parse(Request.Params["CharID"]);
      WGR_Business.GuildEntry _entry = WGR_Business.DnnGuildRosterController.GetCharEntry(_char_ID);

      //_entry.DNN_Officer_Notes = this.DNN_Officer_Notes;
      //_entry.DNN_Player_Notes = this.DNN_Player_Notes;
      if (this.Primary_Char) {
        _entry.Primary_Char_Flag = 1;
      } else {
        _entry.Primary_Char_Flag = 0;
      }

      int _dnnUserID;
      try {
        _dnnUserID = int.Parse(dropDNN_User.SelectedValue);

        if (_dnnUserID > 0) {
          UpdateEntryWithUserID(_dnnUserID);
        }
      } catch {
        _dnnUserID = -1;
      }

      int _altToMainID;
      try {
        _altToMainID = int.Parse(dropAlt_to_Main.SelectedValue);
      } catch {
        _altToMainID = -1;
      }
      _entry.Alt_to_Main_ID = _altToMainID;

      if (Null.IsNull(_char_ID)) {
        //objRosterController.AddEntry();
      } else {
        _entry.Last_Update_Date = DateTime.UtcNow;
        _entry.Last_Update_By = this.UserId;

        WGR_Business.DnnGuildRosterController.UpdateCharEntry(_entry);
        if (_entry.Primary_Char_Flag == 1 && _dnnUserID > 0) {
          WGR_Business.DnnGuildRosterController.AdjustMainCharAcrossUserEntries(_dnnUserID, this.PortalId, _entry.Char_ID);
        } else if (_entry.Alt_to_Main_ID > 0 && _dnnUserID > 0) {
          WGR_Business.DnnGuildRosterController.AdjustMainCharAcrossUserEntries(_dnnUserID, this.PortalId, _entry.Alt_to_Main_ID);
        } else if (_entry.Alt_to_Main_ID > 0 && _dnnUserID < 0) {
          List<WGR_Business.CharEntryUser> _mappings = WGR_Business.DnnGuildRosterController.GetCharUsersByCharEntry(_entry.MainCharacter.Char_ID);
          if (_mappings.Count > 0) {
            int _dnnUserMain = _mappings[0].UserID;

            if (_dnnUserMain > 0) {
              WGR_Business.DnnGuildRosterController.AddCharUserEntry(new WGR_Business.CharEntryUser(_entry.MainCharacter.Char_ID, this.PortalId, _dnnUserMain));
              //WGR_Business.DnnGuildRosterController.AdjustMainCharAcrossUserEntries(_dnnUserID, this.PortalId, _entry.Alt_to_Main_ID);
            }
          }
        }
      }
    }

    private void UpdateEntryWithUserID(int dnnUserID) {
      int _char_ID = Int32.Parse(Request.Params["CharID"]);

      if (_char_ID > 0) {
        WGR_Business.GuildEntry _char = WGR_Business.DnnGuildRosterController.GetCharEntry(_char_ID);
        WGR_Business.GuildEntry _main = _char.MainCharacter;

        //We tried grabbing the main character entry in an attempt to claim the root character
        //  and then recursively get down to the particular character we are now claiming.
        //  Otherwise, we process this guy as we have him right now.
        if (_main.Char_ID > 0) {
          WGR_Business.DnnGuildRosterController.AddCharUserEntry(
            new DotNetWarcraft.WoWGuildRoster.Business.CharEntryUser(_main.Char_ID, this.PortalId, dnnUserID));
        } else {
          WGR_Business.DnnGuildRosterController.AddCharUserEntry(
            new DotNetWarcraft.WoWGuildRoster.Business.CharEntryUser(_char_ID, this.PortalId, dnnUserID));
        }
      }
    }
    #endregion
    #region Private Methods - Character Entry Data
    protected void LoadData_CharInfo(ref WGR_Business.GuildEntry charEntry) {
      this.lblProf_TalentSpec.Text = charEntry.Char_Spec;
      this.imgSpecImage.ImageUrl = "images/IconSmall_" + charEntry.Char_Class.ToLower() + "-" + charEntry.Char_Spec_Num.ToString() + ".png";
      this.hlProf_TalentNumb.Text = charEntry.CharInfo_SpecNumbers;
      this.hlProf_TalentNumb.NavigateUrl = charEntry.ArmoryLink_Talents;
      this.hlProf_TalentNumb.Target = "ArmoryInfo";

      this.Char_Name = charEntry.Char_Name;

      this.hlChar_Name.NavigateUrl = charEntry.ArmoryLink_CharInfo;
      this.hlChar_Name.Target = "ArmoryInfo";

      this.Char_Server = charEntry.Char_Server;

      this.Char_Guild = charEntry.Char_Guild;
      this.Char_Guild_Rank = charEntry.Game_Guild_RankName;

      this.WoW_Guild_Notes = charEntry.Game_Guild_Notes;
      this.WoW_Last_Login = charEntry.Game_Last_Login.ToShortDateString();
      if (charEntry.Last_Load_ID > 0) {
        this.WoW_Last_Load_Time = charEntry.Last_Load_Date.ToShortDateString() + " " + charEntry.Last_Load_Date.ToShortTimeString() + " GMT";
        this.WoW_Last_Load_By = "(Load " + charEntry.Last_Load_ID.ToString() + ")";
      }
      if (charEntry.Last_Load_Date == charEntry.Last_Update_Date) {
        trLastUpdateInfo.Visible = false;
      } else {
        trLastUpdateInfo.Visible = true;
        this.WoW_Last_Update_Time = charEntry.Last_Update_Date.ToShortDateString() + " " + charEntry.Last_Update_Date.ToShortTimeString() + " GMT";
        if (charEntry.Last_Update_By > 0) {
          DotNetNuke.Entities.Users.UserInfo _updater;
          try {
            _updater = DotNetNuke.Entities.Users.UserController.GetUser(this.PortalId, charEntry.Last_Update_By, true);
          } catch { _updater = null; }
          if (_updater != null) {
            this.WoW_Last_Update_By = "(" + _updater.Username + ")";
          }
        } else {
          this.WoW_Last_Update_By = "(System Account)";
        }
      }

      this.WoW_Officer_Notes = charEntry.Game_Officer_Notes;

      this.Char_ClassInfo = "Level " + charEntry.Char_Level.ToString() + " " + charEntry.Char_Class;
      this.Char_RaceGender = ""; // charEntry.Char_Race + " " + charEntry.Char_Gender;

      this.imgCharIcon.ImageUrl = charEntry.CharIcon;
      this.imgCharIcon.Visible = true;

      this.Primary_Char = (charEntry.Primary_Char_Flag > 0);
      this.Alt_to_MainID = charEntry.Alt_to_Main_ID;

      this.LoadData_CharInfo_BaseStats(ref charEntry);
      this.LoadData_CharInfo_Profs(ref charEntry);
      this.LoadData_CharInfo_Resists(ref charEntry);
      this.LoadData_CharInfo_DnnUser(ref charEntry);

      this.BindStatList1DropDown();
      this.BindStatList2DropDown();

      this.LoadData_CharInfo_Stats(ref charEntry);
      this.LoadData_CharInfo_ArenaPvP(ref charEntry);

      //Code to generate Signature graphic - for future release
      //System.Drawing.Bitmap _charGraphic = charEntry.SigGraphic;
      //System.Drawing.Imaging.EncoderParameters _params = new System.Drawing.Imaging.EncoderParameters(1);
      //_params.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);
      //System.Drawing.Imaging.ImageCodecInfo _jpgCodec = WoWGuildRoster.Business.DNW_WoWGuildRoster.GetEncoderInfo("image/jpeg");

      //_charGraphic.Save(Server.MapPath("generated_image.jpg"), _jpgCodec, _params);

    }
    protected void LoadData_CharInfo_BaseStats(ref WGR_Business.GuildEntry charEntry) {
      lblStat_HealthLabel.Visible = false;
      lblStat_Health.Visible = false;
      lblStat_HealthBr.Visible = false;

      lblStat_SecondaryLabel.Visible = false;
      lblStat_Secondary.Visible = false;
      lblStat_SecondaryBr.Visible = false;

      List<WGR_Business.GuildEntryStat> _stats = charEntry.BaseStats;
      foreach (WGR_Business.GuildEntryStat _base in _stats) {
        switch (_base.Stat_Entry) {
          case "HealthBarTotal":
            lblStat_HealthLabel.Text = "Health";
            lblStat_Health.Text = _base.Stat_Value_int.ToString();

            lblStat_HealthLabel.Visible = true;
            lblStat_Health.Visible = true;
            lblStat_HealthBr.Visible = true;
            break;
          case "SecondBarTotal":
            lblStat_SecondaryLabel.Text = _base.Stat_Value_str.ToString();
            lblStat_Secondary.Text = _base.Stat_Value_int.ToString();

            if (lblStat_SecondaryLabel.Text.ToLower() == "mana" ||
                lblStat_SecondaryLabel.Text.ToLower() == "energy") {
              lblStat_SecondaryLabel.Visible = true;
              lblStat_Secondary.Visible = true;
              lblStat_SecondaryBr.Visible = true;
            }
            break;
        }
      }

    }
    protected void LoadData_CharInfo_Profs(ref WGR_Business.GuildEntry charEntry) {
      if (charEntry.Char_Prof1 == "") {
        this.lblProf_Prof1Label.Text = "None";
        this.lblProf_Prof1.Visible = false;
        this.imgProf1Icon.Visible = false;
      } else {
        this.lblProf_Prof1Label.Text = charEntry.Char_Prof1;
        this.lblProf_Prof1.Text = charEntry.Char_Prof1_Level.ToString();
        this.imgProf1Icon.AlternateText = charEntry.Char_Prof1;
        this.imgProf1Icon.ImageUrl = "images/IconSmall_" + charEntry.Char_Prof1.ToLower() + ".png";
      }
      if (charEntry.Char_Prof2 == "") {
        this.lblProf_Prof2Label.Text = "None";
        this.lblProf_Prof2.Visible = false;
        this.imgProf2Icon.Visible = false;
      } else {
        this.lblProf_Prof2Label.Text = charEntry.Char_Prof2;
        this.lblProf_Prof2.Text = charEntry.Char_Prof2_Level.ToString();
        this.imgProf2Icon.AlternateText = charEntry.Char_Prof2;
        this.imgProf2Icon.ImageUrl = "images/IconSmall_" + charEntry.Char_Prof2.ToLower() + ".png";
      }
    }
    protected void LoadData_CharInfo_Resists(ref WGR_Business.GuildEntry charEntry) {

      List<WGR_Business.GuildEntryStat> _resists = charEntry.Resistances;
      foreach (WGR_Business.GuildEntryStat _resist in _resists) {
        switch (_resist.Stat_Entry) {
          case "Arcane":
            lblProf_ResArcane.Text = _resist.Stat_Value_int.ToString();
            break;
          case "Fire":
            lblProf_ResFire.Text = _resist.Stat_Value_int.ToString();
            break;
          case "Frost":
            lblProf_ResFrost.Text = _resist.Stat_Value_int.ToString();
            break;
          case "Holy":
            lblProf_ResHoly.Text = _resist.Stat_Value_int.ToString();
            break;
          case "Nature":
            lblProf_ResNature.Text = _resist.Stat_Value_int.ToString();
            break;
          case "Shadow":
            lblProf_ResShadow.Text = _resist.Stat_Value_int.ToString();
            break;
        }
      }

    }
    protected void LoadData_CharInfo_DnnUser(ref WGR_Business.GuildEntry charEntry) {
      this.DNN_UserID = -1;

      List<WGR_Business.CharEntryUser> _mappings = WGR_Business.DnnGuildRosterController.GetCharUsersByCharEntry(charEntry.Char_ID);
      if (_mappings.Count > 1) {
        (new Business.Exceptions.WGRGeneralError(new ApplicationException("DotNetWarcraft.WoWGuildRoster.UI.DNN.WGR_CharEntry.Page_Load(char " + charEntry.Char_ID.ToString() + " - " + charEntry.Char_Name + ") - Returned " + _mappings.Count.ToString() + " mappings."))).WriteExceptionToDB();
      } else if (_mappings.Count == 1) {
        this.DNN_UserID = _mappings[0].UserID;
      }

    }
    protected void LoadData_CharInfo_Stats(ref WGR_Business.GuildEntry charEntry) {

      //Load the selected group of stats
      String _statList = drpStatList1.SelectedValue;
      chslStatList.StatType = _statList;
      switch (_statList) {
        case WGR_Business.WGRDnnConstants.ASCX_CHARENTRY_STATTYPE_BASE:
          chslStatList.Visible = true;
          chslStatList.StatList = charEntry.Abilities;
          break;
        case WGR_Business.WGRDnnConstants.ASCX_CHARENTRY_STATTYPE_MELEE:
          chslStatList.Visible = true;
          chslStatList.StatList = charEntry.MeleeSkills;
          break;
        case WGR_Business.WGRDnnConstants.ASCX_CHARENTRY_STATTYPE_SPELL:
          chslStatList.Visible = true;
          chslStatList.StatList = charEntry.SpellSkills;
          break;
        case WGR_Business.WGRDnnConstants.ASCX_CHARENTRY_STATTYPE_RANGED:
          chslStatList.Visible = true;
          chslStatList.StatList = charEntry.RangedSkills;
          break;
        case WGR_Business.WGRDnnConstants.ASCX_CHARENTRY_STATTYPE_DEFENSE:
          chslStatList.Visible = true;
          chslStatList.StatList = charEntry.DefenseSkills;
          break;
        default:
          chslStatList.Visible = false;
          break;
      }


      //Load the selected reputation group
      String _repGroup = drpStatList2.SelectedValue;

      if (_repGroup.Trim() != "") {
        slRepList.Visible = true;

        List<WGR_Business.GuildEntryStat> _reps = charEntry.Reputations_ByGroup(_repGroup);
        slRepList.ParentModuleID = this.ModuleId;
        slRepList.StatList = _reps;
      } else {
        slRepList.Visible = false;
      }
    }
    protected void LoadData_CharInfo_ArenaPvP(ref WGR_Business.GuildEntry charEntry) {

      List<WGR_Business.GuildEntryStat> _arenaStats = charEntry.ArenaTeamInfo;
      foreach (WGR_Business.GuildEntryStat _stat in _arenaStats) {
        switch (_stat.Stat_Group) {
          case "Team2v2":
            trPvp2v2.Visible = true;
            trPvpNoData.Visible = false;
            switch (_stat.Stat_Entry) {
              case "TeamName":
                lbl2v2Name_value.Text = _stat.Stat_Value_str.ToString();
                break;
              case "TeamRanking":
                if (_stat.Stat_Value_int == 0) {
                  lbl2v2Rank_value.Text = "Not Ranked";
                  } else {
                  lbl2v2Rank_value.Text = _stat.Stat_Value_int.ToString();
                  }
                break;
              case "PersonalRating":
                lbl2v2Rate_value.Text = _stat.Stat_Value_int.ToString();
                break;
              case "WinsGamesWeek":
                lbl2v2GaWW_value.Text = _stat.Stat_Value_str.ToString();
                break;
              case "WinsGamesSeason":
                lbl2v2GaWS_value.Text = _stat.Stat_Value_str.ToString();
                break;
              }
            break;
          case "Team3v3":
            trPvp3v3.Visible = true;
            trPvpNoData.Visible = false;
            switch (_stat.Stat_Entry) {
              case "TeamName":
                lbl3v3Name_value.Text = _stat.Stat_Value_str.ToString();
                break;
              case "TeamRanking":
                if (_stat.Stat_Value_int == 0) {
                  lbl3v3Rank_value.Text = "Not Ranked";
                  } else {
                  lbl3v3Rank_value.Text = _stat.Stat_Value_int.ToString();
                  }
                break;
              case "PersonalRating":
                lbl3v3Rate_value.Text = _stat.Stat_Value_int.ToString();
                break;
              case "WinsGamesWeek":
                lbl3v3GaWW_value.Text = _stat.Stat_Value_str.ToString();
                break;
              case "WinsGamesSeason":
                lbl3v3GaWS_value.Text = _stat.Stat_Value_str.ToString();
                break;
              }

            break;
          case "Team5v5":
            trPvp5v5.Visible = true;
            trPvpNoData.Visible = false;
            switch (_stat.Stat_Entry) {
              case "TeamName":
                lbl5v5Name_value.Text = _stat.Stat_Value_str.ToString();
                break;
              case "TeamRanking":
                if (_stat.Stat_Value_int == 0) {
                  lbl5v5Rank_value.Text = "Not Ranked";
                  } else {
                  lbl5v5Rank_value.Text = _stat.Stat_Value_int.ToString();
                  }
                break;
              case "PersonalRating":
                lbl5v5Rate_value.Text = _stat.Stat_Value_int.ToString();
                break;
              case "WinsGamesWeek":
                lbl5v5GaWW_value.Text = _stat.Stat_Value_str.ToString();
                break;
              case "WinsGamesSeason":
                lbl5v5GaWS_value.Text = _stat.Stat_Value_str.ToString();
                break;
              }

            break;
          }
        }

      }

    #endregion
    #region "Event Handlers - Command Buttons"
    protected void cmdUpdate_Click(object sender, EventArgs e) {
      try {
        if (Page.IsValid == true) {
          this.UpdateEntryToDB();
          Response.Redirect(Globals.NavigateURL(), true);
        }
      } catch (Exception exc) {
        Exceptions.ProcessModuleLoadException(this, exc);
      }
    }

    protected void cmdCancel_Click(object sender, EventArgs e) {
      try {
        Response.Redirect(Globals.NavigateURL(), true);
      } catch (Exception exc) {
        Exceptions.ProcessModuleLoadException(this, exc);
      }
    }

    protected void cmdDelete_Click(object sender, EventArgs e) {
      try {
        int _char_ID = Int32.Parse(Request.Params["CharID"]);
        if (!Null.IsNull(_char_ID)) {
          WGR_Business.DnnGuildRosterController.DeleteCharEntry(_char_ID);
        }
        Response.Redirect(Globals.NavigateURL(), true);
      } catch (Exception exc) {
        Exceptions.ProcessModuleLoadException(this, exc);
      }
    }

    protected void cmdClaimEntry_Click(object sender, EventArgs e) {
      UpdateEntryWithUserID(this.UserId);
      Response.Redirect(Request.Url.ToString(), true);
    }

    protected void cmdThisIsMyMain_Click(object sender, EventArgs e) {
      //First - Save off what we have here
      this.UpdateEntryToDB();

      //Next - Let's make it the main character among linked
      int _char_ID = Int32.Parse(Request.Params["CharID"]);

      WGR_Business.GuildEntry _char = WGR_Business.DnnGuildRosterController.GetCharEntry(_char_ID);
      _char.MakePrimaryCharacter();

      //Then - Let's claim this user and all alts, just in case
      UpdateEntryWithUserID(this.UserId);

      //Lastly - let's make this character the main for all claimed characters
      WGR_Business.DnnGuildRosterController.AdjustMainCharAcrossUserEntries(this.UserId, this.PortalId, _char_ID);

      //And let's refresh the page for the user
      Response.Redirect(Request.Url.ToString(), true);
    }
    #endregion

    protected void btnTestDisplayOptions_Refresh_Click(object sender, EventArgs e) {
      this.RetrieveVisibilityRoles();
      this.TrimVisibility();
      this.BindAltList();
    }

    protected void drpStatList1_SelectedIndexChanged(object sender, EventArgs e) {
      int _char_ID = Int32.Parse(Request.Params["CharID"]);
      WGR_Business.GuildEntry _char = WGR_Business.DnnGuildRosterController.GetCharEntry(_char_ID);
      this.LoadData_CharInfo_Stats(ref _char);

    }

    protected void drpStatList2_SelectedIndexChanged(object sender, EventArgs e) {
      int _char_ID = Int32.Parse(Request.Params["CharID"]);
      WGR_Business.GuildEntry _char = WGR_Business.DnnGuildRosterController.GetCharEntry(_char_ID);
      this.LoadData_CharInfo_Stats(ref _char);
    }

    protected void rstStatList2_Load(object sender, EventArgs e) {

    }

  }
}